package com.tool.finances.lifestyle;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;

import com.tool.finances.lifestyle.data.AccountProvider;
import com.tool.finances.lifestyle.data.AccountType;

public class AddCategory extends Activity {

	private EditText contentText;
	private ImageButton buttonCancel;
	private ImageButton buttonOk;

	private int ID = 0;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addcategory);
		setTitle("");

		ID = getIntent().getExtras().getInt("AccountTag");

		contentText = (EditText) findViewById(R.id.CategoryContentText);
		buttonCancel = (ImageButton) findViewById(R.id.CategoryCancel);
		buttonOk = (ImageButton) findViewById(R.id.CategoryOk);

		buttonCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		buttonOk.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!contentText.getText().toString().equalsIgnoreCase("")) {
					ContentValues values = new ContentValues();
					values.put(AccountType.Account.CATEGORYTYPE, contentText
							.getText().toString());
					try {
						getContentResolver().insert(
								AccountProvider.CATEGORY_CONTENT_URI, values);
					} catch (Exception e) {
						Log.println(1, "Category:", "add error.");
					}

					Intent i = new Intent(AddCategory.this,
							AccountActivity.class);
					i.putExtra("AccountCategory", contentText.getText()
							.toString());
					i.putExtra("AccountTag", ID);
					startActivity(i);
					finish();
				}
			}
		});
	}

}
